﻿namespace HuaWei.JLReport
{
    using HuaWei;
    using HuaWei.CommClass;
    using HuaWei.DAI;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;

    internal static class RepUtils
    {
        public static void CreateAllRepData(string bdid)
        {
            IDataAccess ida = DBHelp.IniIDA();
            string sql = string.Format("select * from xmqc where bdid='{0}'", bdid);
            DataTable table = DBHelp.GetDt(ida, sql, "dtTemp");
            string xmid = "";
            string yzid = "";
            foreach (DataRow row in table.Rows)
            {
                if (row["zt"].Equals(-1))
                {
                    xmid = row["xmid"].ToString();
                    yzid = row["yzid"].ToString();
                    CreateRepData(bdid, row["qcid"].ToString(), xmid, yzid, false);
                }
            }
        }

        public static void CreateRepData(string bdid, string qcid, string xmid, string yzid, [Optional, DefaultParameterValue(true)] bool checkZT)
        {
            string str;
            IDataAccess ida = DBHelp.IniIDA();
            if (checkZT)
            {
                str = string.Format("select * from xmqc where bdid='{0}' and qcid='{1}'", bdid, qcid);
                if (!DBHelp.GetDt(ida, str, "dtTemp").Rows[0]["zt"].Equals(-1))
                {
                    return;
                }
            }
            try
            {
                ida.BeginTrans();
                DataSet ds = new DataSet();
                IniExportData(qcid, bdid, xmid, yzid);
                CreateZQZFZS(ida, qcid, bdid);
                UpdateZQZFZS(ida, ds, qcid, bdid);
                UpdateGCBG(ida, ds, qcid, bdid);
                UpdateRefBGL(ida, ds, qcid, bdid);
                if (ds.HasChanges())
                {
                    ida.Update(ds, true);
                }
                InsertHZ(ida, qcid, bdid);
                str = string.Format("update xmqc set zt=0 where bdid='{0}' and qcid='{1}'", bdid, qcid);
                DBHelp.ExeSql(ida, str);
                ida.Commit();
            }
            catch
            {
                ida.RollBack();
            }
        }

        public static void CreateZQZFZS(IDataAccess ida, string qcid, string bdid)
        {
            string sql = string.Format(CRepSQL.DEL_ZQZFZS, bdid, qcid);
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_QD, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_QD1, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_QD2, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = string.Format(CRepSQL.UPDATE_ZQZFZS, bdid, qcid);
            DBHelp.ExeSql(ida, sql);
            sql = "update REP_ZQZFZS set zjlx=substring(qdbh,1,1) where bdid='{0}' and qcid={1}";
            sql = string.Format(sql, bdid, qcid);
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_JGTZ, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_SPJE, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_WYFJ, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_CFKLX, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_DYYFK, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_DYYFK_KC, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_CLYFK, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_CLYFK_KC, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = string.Format(CRepSQL.DEL_GCBG, bdid, qcid);
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_GCBG_QD, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
        }

        public static void IniExportData(string qcid, string bdid, string xmid, string yzid)
        {
            DataRow row;
            IDataAccess ida = DBHelp.IniIDA();
            DataSet ds = new DataSet();
            string sql = string.Format("select * from QTJL_DYYFK where bdid='{0}' and qcid='{1}'", bdid, qcid);
            DBHelp.ExecuteDataSet(ds, sql, "QTJL_DYYFK");
            DataTable table = ds.Tables["QTJL_DYYFK"];
            if (table.Rows.Count == 0)
            {
                row = table.NewRow();
                row["xmid"] = xmid;
                row["yzid"] = yzid;
                row["bdid"] = bdid;
                row["qcid"] = qcid;
                table.Rows.Add(row);
            }
            else
            {
                row = table.Rows[0];
            }
            row["qcid"] = qcid;
            SetDYYFK(ida, row, bdid, qcid);
            Log.Write("SetDYYFK(idaClient, row) End");
            sql = string.Format("select * from QTJL_CLYFK  where bdid='{0}' and qcid='{1}'", bdid, qcid);
            DBHelp.ExecuteDataSet(ds, sql, "QTJL_CLYFK");
            table = ds.Tables["QTJL_CLYFK"];
            if (table.Rows.Count == 0)
            {
                row = table.NewRow();
                row["xmid"] = xmid;
                row["yzid"] = yzid;
                row["bdid"] = bdid;
                row["qcid"] = qcid;
                table.Rows.Add(row);
            }
            else
            {
                row = table.Rows[0];
            }
            row["qcid"] = qcid;
            SetCLYFK(ida, row, qcid, bdid);
            ida.Update(ds, true);
            Log.Write("IniExportData idaClient.Update(ds) End");
        }

        public static void InsertHZ(IDataAccess ida, string qcid, string bdid)
        {
            string sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_XJ, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_HJ, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
            sql = CHelp.smethod(string.Format(CRepSQL.INSERT_ZQZFZS_ZF, bdid, qcid));
            DBHelp.ExeSql(ida, sql);
        }

        public static void SetCLYFK(IDataAccess ida, DataRow row, string qcid, string bdid)
        {
            string sql = "";
            int num = CHelp.ConvertInt(qcid, 0);
            sql = string.Format("select * from ZZJGXXEX where id='{0}'", bdid);
            GQKK gqkk = new GQKK(DBHelp.GetDt(ida, sql, "ZZJGXXEX").Rows[0]["KCLYYKGS"].ToString());
            new DataSet();
            int gQ = gqkk.GQ;
            int kHJDQ = gqkk.KHJDQ;
            decimal num4 = 0M;
            decimal num5 = 0M;
            decimal num6 = 0M;
            decimal num7 = 0M;
            decimal num8 = 0M;
            decimal num9 = DHelp.ToDecimal(row["BQDFJE"]);
            DataTable dt = DBHelp.GetDt(string.Format("select * from QTJL_CLYFK where bdid='{0}' and qcid={1}-1", bdid, num), "QTJL_CLYFK");
            if (dt.Rows.Count > 0)
            {
                num7 = CHelp.ConvertDecimal(dt.Rows[0]["LJDFJE"], 0, 0);
            }
            if ((num > 1) && ((num - gQ) > 0))
            {
                sql = string.Format("select * from QTJL_CLYFK  where bdid='{0}' and qcid>{1} and qcid<={2}", bdid, (num - kHJDQ) - gQ, num - kHJDQ);
                DataTable table3 = DBHelp.GetDt(ida, sql, "QTJL_CLYFK");
                int num10 = 0;
                int num11 = 0;
                foreach (DataRow row2 in table3.Rows)
                {
                    num10 = Convert.ToInt16(row2["qcid"]);
                    num11 = Convert.ToInt16((int) (Convert.ToInt16(row2["BQDFJE"]) / kHJDQ));
                    if (((num10 - gQ) - kHJDQ) == 0)
                    {
                        num4 += Convert.ToInt16(row2["BQDFJE"]) - (num11 * (kHJDQ - 1));
                    }
                    else
                    {
                        num4 += num11;
                    }
                }
                sql = string.Format("select * from QTJL_CLYFK where bdid='{0}' and qcid='{1}'", bdid, num - 1);
                DataTable table4 = DBHelp.GetDt(ida, sql, "QTJL_CLYFK");
                if (table4.Rows.Count > 0)
                {
                    num5 = DHelp.ToDecimal(table4.Rows[0]["BQMKHJE"]);
                    num7 = DHelp.ToDecimal(table4.Rows[0]["LJDFJE"]);
                }
                else
                {
                    num5 = 0M;
                    num7 = 0M;
                }
            }
            num6 = num4 + num5;
            num8 = num9 + num7;
            row["BQKHJE"] = num4;
            row["SQMKHJE"] = num5;
            row["BQMKHJE"] = num6;
            row["SQMDFJE"] = num7;
            row["BQDFJE"] = num9;
            row["LJDFJE"] = num8;
            row.EndEdit();
        }

        public static void SetDYYFK(IDataAccess ida, DataRow bqRow, string bdid, string qcid)
        {
            Log.Write("SetDYYFK Begin");
            string sql = null;
            decimal num = 0M;
            decimal num2 = 0M;
            decimal num3 = 0M;
            decimal num4 = 0M;
            decimal num5 = 0M;
            decimal num7 = 0M;
            sql = string.Format("select KDYYFKGS from ZZJGXXEX  where id='{0}'", bdid);
            BZGSKH bzgskh = new BZGSKH(DBHelp.GetDt(ida, sql, "t_zzjg").Rows[0]["KDYYFKGS"].ToString());
            sql = string.Format("select sum(htje) as htje from jlqd where ZBCD=0 and CJM=0 and bdid='{0}'", bdid);
            DataTable table2 = DBHelp.GetDt(ida, sql, "dtTemp");
            decimal num8 = 0M;
            if (table2.Rows.Count > 0)
            {
                num8 = CHelp.ConvertDecimal(CHelp.ConvertDecimal(table2.Rows[0][0], 0, 0), 0, 0);
            }
            bzgskh.HTZJ = num8;
            sql = "\r\nselect sum(a.bqjls*b.zbdj) as dj from (\r\nselect qdbh,bqjls from qdjl_mx where bdid='{0}' and qcid<={1}\r\nunion all\r\nselect qdbh,bqjls from bgqdjl_mx where bdid='{0}' and qcid<={1}\r\n) as a left join jlqd as b on a.qdbh=b.qdbh where b.bdid='{0}'\r\n";
            sql = string.Format(sql, bdid, qcid);
            DataTable table3 = DBHelp.GetDt(ida, sql, "dtQDJL");
            if (table3.Rows.Count > 0)
            {
                num7 = CHelp.ConvertDecimal(table3.Rows[0][0], 0, 0);
            }
            sql = string.Format("select sum(BQJLS) as BQJLS from QDJL_MX  where bdid='{0}' and qcid<={1}", bdid, qcid);
            num5 = DHelp.GetValue(DBHelp.GetDt(ida, sql, "T_BQJLS").Rows[0], "BQJLS");
            if (Convert.ToInt16(qcid) > 1)
            {
                sql = string.Format("select * from QTJL_DYYFK  where bdid='{0}' and qcid={1}-1", bdid, qcid);
                DataTable table5 = DBHelp.GetDt(ida, sql, "SQDYYFK");
                if (table5.Rows.Count > 0)
                {
                    DataRow row = table5.Rows[0];
                    num = CHelp.ConvertDecimal(row["BQMWC"], 0, 0);
                    num2 = CHelp.ConvertDecimal(row["BQMZFDYYFK"], 0, 0);
                    num3 = CHelp.ConvertDecimal(row["BQMKCDYYFK"], 0, 0);
                }
                else
                {
                    num = 0M;
                    num2 = 0M;
                    num3 = 0M;
                }
            }
            if (bzgskh.QKE.Equals((decimal) 0M))
            {
                bzgskh.QKE = CHelp.ConvertDecimal(((bzgskh.HTZJ - bzgskh.ZDJE) * bzgskh.QKEBL) / 100M, 0, 0);
            }
            decimal num10 = CHelp.ConvertDecimal(bqRow["BQSQDYYFK"], 0, 0);
            bqRow["YFDYYFK"] = num2;
            bqRow["BQMZFDYYFK"] = num10 + num2;
            bqRow["HTZJ"] = bzgskh.HTZJ;
            bqRow["DYYFKZE"] = bzgskh.DYYFKZE;
            bqRow["QKE"] = bzgskh.QKE;
            decimal decimal1 = bzgskh.QKEBL / 100M;
            if (num5 > bzgskh.QKE)
            {
                decimal num11 = 0M;
                if (!bzgskh.HTZJ.Equals((decimal) 0M))
                {
                    num11 = ((num2 * (num5 - bzgskh.QKE)) * 2M) / bzgskh.HTZJ;
                }
                if (num11 > num2)
                {
                    num4 = num2 - num3;
                }
                else
                {
                    num4 = num11 - num3;
                }
                if (num4 < 0M)
                {
                    num4 = 0M;
                }
            }
            bqRow["BQKCDYYFK"] = num4;
            bqRow["SQMWC"] = num;
            bqRow["BQMWC"] = num + num4;
            bqRow["BQWC"] = num7;
            bqRow.EndEdit();
            Log.Write("SetDYYFK End");
        }

        public static void UpdateGCBG(IDataAccess ida, DataSet ds, string qcid, string bdid)
        {
            string exeStr = "";
            exeStr = string.Format(CRepSQL.GCBG, bdid, qcid);
            ds.Merge(ida.ExecuteDataSet(exeStr, CommandType.Text, "REP_GCBG"));
            DataTable table = ds.Tables["REP_GCBG"];
            exeStr = string.Format(CRepSQL.GCBG_1, bdid, qcid);
            DataTable table2 = DBHelp.GetDt(ida, exeStr, "dtPZH");
            exeStr = string.Format(CRepSQL.GCBG_2, bdid, qcid);
            DataTable table3 = DBHelp.GetDt(ida, exeStr, "dtBGLH");
            foreach (DataRow row in table.Rows)
            {
                DataRow row2;
                DataRow row3;
                table2.DefaultView.RowFilter = "qdbh='" + row["qdbh"].ToString() + "'";
                foreach (DataRowView view in table2.DefaultView)
                {
                    row2 = view.Row;
                    if (!row["pzbh"].ToString().Trim().Equals(""))
                    {
                        (row3 = row)["pzbh"] = row3["pzbh"] + "/";
                    }
                    (row3 = row)["pzbh"] = row3["pzbh"] + row2["pzbh"].ToString();
                }
                table3.DefaultView.RowFilter = "qdbh='" + row["qdbh"].ToString() + "'";
                foreach (DataRowView view in table3.DefaultView)
                {
                    row2 = view.Row;
                    if (!row["zh"].ToString().Trim().Equals(""))
                    {
                        (row3 = row)["zh"] = row3["zh"] + "/";
                    }
                    (row3 = row)["zh"] = row3["zh"] + row2["bglh"].ToString();
                }
            }
        }

        public static void UpdateRefBGL(IDataAccess ida, DataSet ds, string qcid, string bdid)
        {
            string exeStr = string.Format("delete from REFBGL where bdid='{0}' and qcid={1}", bdid, qcid);
            ida.ExecuteNonQuery(exeStr, CommandType.Text);
            exeStr = string.Format("insert into REFBGL(id,bdid,qcid,bglid) select id,bdid,{0},id from bgl where bdid='{1}' and tysb=1", qcid, bdid);
            ida.ExecuteNonQuery(exeStr, CommandType.Text);
        }

        public static void UpdateZJJE(IDataAccess ida, string qcid, string bdid)
        {
            string format = "select zjlx,sum(htje) as htje from jlqd where bdid='{0}' group by zjlx";
            format = string.Format(format, bdid);
            foreach (DataRow row in DBHelp.GetDt(ida, format, "dtTemp").Rows)
            {
                format = string.Format("update REP_ZQZFZS set htje={3},bghje={3} where yjd=-1 and bdid='{0}' and qcid={1} and zjlx='{2}'", new object[] { bdid, qcid, row["zjlx"].ToString(), row["htje"].ToString() });
                DBHelp.ExeSql(ida, format);
            }
        }

        public static void UpdateZQZFZS(IDataAccess ida, DataSet ds, string qcid, string bdid)
        {
            string exeStr = "";
            exeStr = CHelp.smethod(string.Format(CRepSQL.ZQZFZS, bdid, qcid));
            ds.Merge(ida.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQZFZS"));
            DataTable table = ds.Tables["REP_ZQZFZS"];
            exeStr = CHelp.smethod(string.Format(CRepSQL.ZQZFZS_1, bdid, qcid));
            DataTable table2 = DBHelp.GetDt(ida, exeStr, "dtPZH");
            exeStr = string.Format(CRepSQL.ZQZFZS_2, bdid, qcid);
            DataTable table3 = DBHelp.GetDt(ida, exeStr, "dtZH");
            exeStr = CHelp.smethod(string.Format(CRepSQL.ZQZFZS_BZ, bdid, qcid));
            DataTable table4 = DBHelp.GetDt(ida, exeStr, "ZQZFZS_BZ");
            foreach (DataRow row in table.Rows)
            {
                DataRow row2;
                DataRow row3;
                table2.DefaultView.RowFilter = "qdbh='" + row["qdbh"].ToString() + "'";
                foreach (DataRowView view in table2.DefaultView)
                {
                    row2 = view.Row;
                    if (!row["pzbh"].ToString().Trim().Equals(""))
                    {
                        (row3 = row)["pzbh"] = row3["pzbh"] + "/";
                    }
                    (row3 = row)["pzbh"] = row3["pzbh"] + row2["pzbh"].ToString();
                }
                table3.DefaultView.RowFilter = "qdbh='" + row["qdbh"].ToString() + "'";
                foreach (DataRowView view in table3.DefaultView)
                {
                    row2 = view.Row;
                    if (!row["zh"].ToString().Trim().Equals(""))
                    {
                        (row3 = row)["zh"] = row3["zh"] + "/";
                    }
                    (row3 = row)["zh"] = row3["zh"] + row2["qqzh"].ToString();
                }
                string str2 = row["qdbh"].ToString();
                if ((str2.Length.Equals(3) || str2.Equals("D")) || str2.Equals("Z"))
                {
                    if (str2.Equals("D") || str2.Equals("Z"))
                    {
                        str2 = str2 + "00";
                    }
                    table4.DefaultView.RowFilter = "bz='" + str2 + "'";
                    foreach (DataRowView view in table4.DefaultView)
                    {
                        row2 = view.Row;
                        row["BQJLJE"] = row2["BQJLJE"];
                        row["SQMJLJE"] = row2["SQMJLJE"];
                        row["BQMJLJE"] = row2["BQMJLJE"];
                    }
                }
            }
        }
    }
}

